package com.dmitriyp.movie.console;

import static java.lang.System.out;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.dmitriyp.movie.console.actions.UserAddAction;
import com.dmitriyp.movie.console.actions.UserDeleteAction;
import com.dmitriyp.movie.console.actions.UserFilterAction;
import com.dmitriyp.movie.console.actions.UserFilterByStarAction;
import com.dmitriyp.movie.console.actions.UserImportAction;
import com.dmitriyp.movie.console.actions.UserListAction;
import com.dmitriyp.movie.console.actions.UserViewAction;
import com.dmitriyp.movie.dao.IMovieManager;
import com.dmitriyp.movie.dao.MovieManagerFactory;
import com.dmitriyp.movie.dao.Order;
import com.dmitriyp.movie.dao.Order.Type;

public class Application {


    public static void main(String[] args) throws ClassNotFoundException {
        new Application().start();
    }

    private Map<Integer, IUserAction> actions = new HashMap<Integer, IUserAction>();

    public Application() {
        IMovieManager manager = new MovieManagerFactory().createMovieManager();
        actions.put(1, new UserAddAction(manager));
        actions.put(2, new UserListAction(manager, new Order(Type.ASC, "title")));
        actions.put(3, new UserListAction(manager, new Order(Type.ASC, "releaseYear")));
        actions.put(4, new UserViewAction(manager));
        actions.put(5, new UserDeleteAction(manager));
        actions.put(6, new UserFilterAction(manager));
        actions.put(7, new UserFilterByStarAction(manager));
        actions.put(8, new UserImportAction(manager));
    }

    public void start() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            out.println("Chose operation:");
            out.println("1 - Add");
            out.println("2 - List(Title)");
            out.println("3 - List(Year)");
            out.println("4 - View");
            out.println("5 - Delete");
            out.println("6 - Search by Title");
            out.println("7 - Search by Star");
            out.println("8 - Import from XML");
            out.println("9 - Exit");
            int value = scanner.nextInt();
            IUserAction action = actions.get(value);
            if (action == null)
                break;
            else
                action.doAction();
        }
        scanner.close();
    }

}
